<?php
require_once 'db.php';
class User{
		public $name;
		public $pass;
		public $role;

		function get($name=null){
			global $db;
			if($name == null)
				return false;
			$query = $db->select('dbusers',array('name'=>$name));
			if($query){
				$rs = $db->db_fetch_array($query);
				$this->name = $rs['name'];
				$this->pass = $rs['pass'];
				$this->role = $rs['role'];
			}
			else
				return false;
		}
		function User($name=null){
			$this->get($name);
		}

		/**
		 * must get the user first to remove the user
		 */
		function remove(){
			global $db;
			return $db->remove('dbusers',array('name'=>$this->name));
		}
		function add(){
			global $db;
			$update = array(
				'name' => $this->name,
				'pass' => $this->pass,
				'role' => $this->role
			);
			return $db->insert('dbusers',$update);
		}
		function update(){
			global $db;
			$update = array(
				'name' => $this->name,
				'pass' => $this->pass,
				'role' => $this->role
			);
			return $db->update('dbusers',$update,array('name'=>$this->name));
		}
		
		public static function isAdmin($name) {
			global $db;
			$query = $db->select('dbusers',array('name'=>$name));
			$rs = $db->db_fetch_array($query);
			if (isset($rs['role']) && $rs['role'] == ADMIN)
				return true;
			else
				return false;
		}

	}
?>